/* Copyright (c) 2007 Easter 
 * Author Yichao Zhang
 * Email Yichao.Zhang@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ie.ucc.cs.anggao.finalproject.server.blogPost;

import java.io.IOException;
import java.net.URL;

import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.Person;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.util.ServiceException;

public class GoogleBloggerClient {
	private String username = "";

	private String password = "";

	private String feedUri = "";

	private static final String METAFEED_URL = "http://www.blogger.com/feeds/default/blogs";

	private static final String FEED_URI_BASE = "http://www.blogger.com/feeds";
	private static final String POSTS_FEED_URI_SUFFIX = "/posts/default";
	private static final String COMMENTS_FEED_URI_SUFFIX = "/comments/default";

	public GoogleBloggerClient(String username, String password) {
		this.username = username;
		this.password = password;

	}

	public void newPost(String title, String content, Boolean isdraft)
			throws Exception {
		// Proxy

		System.setProperty("https.proxyHost", "10.51.255.250");
		System.setProperty("https.proxyPort", "8080");
		System.setProperty("http.proxyHost", "10.51.255.250");
		System.setProperty("http.proxyPort", "8080");

		BloggerService myService = new BloggerService("Google Blogger");

		// Authenticate using ClientLogin
		myService.setUserCredentials(this.username, this.password);

		// Get the blog ID from the metatfeed.
		String blogId = getBlogId(myService);
		feedUri = FEED_URI_BASE + "/" + blogId;

		// Demonstrate how to create a draft/publish post.
		Entry Post = createPost(myService, title, content, username, username,
				isdraft);

		System.out.println("Successfully created public post: "
				+ Post.getTitle().getPlainText());
	}

	private static String getBlogId(BloggerService myService)
			throws ServiceException, IOException {
		final URL feedUrl = new URL(METAFEED_URL);
		Feed resultFeed = myService.getFeed(feedUrl, Feed.class);

		// If the user has a blog then return the id (which comes after 'blog-')
		if (resultFeed.getEntries().size() > 0) {
			Entry entry = resultFeed.getEntries().get(0);
			return entry.getId().split("blog-")[1];
		}
		throw new IOException("User has no blogs!");
	}

	private Entry createPost(BloggerService myService, String title,
			String content, String authorName, String userName, Boolean isDraft)
			throws ServiceException, IOException {
		// Create the entry to insert
		Entry myEntry = new Entry();
		myEntry.setTitle(new PlainTextConstruct(title));
		myEntry.setContent(new PlainTextConstruct(content));
		Person author = new Person(authorName, null, userName);
		myEntry.getAuthors().add(author);
		myEntry.setDraft(isDraft);

		// Ask the service to insert the new entry
		URL postUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);
		return myService.insert(postUrl, myEntry);
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public static void main(String[] args) {

	}
}
